package org.ssmd3.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.ssmd3.pojo.UserType;
import org.ssmd3.pojo.Users;
import org.ssmd3.service.UserTypeService;
import org.ssmd3.service.UsersService;
import org.ssmd3.util.PageTool;

import javax.annotation.Resource;
import java.util.List;

@Controller
@RequestMapping("/users")
public class UsersController {
    @Resource
    UsersService usersService;
    @Resource
    UserTypeService userTypeService;
    //实现分页
    @RequestMapping("/list")
    public String list(Model model, Users users) {
//        得到总记录数
        int totalRecordCount = usersService.getRecordCount(users);
//        创建PageTool实例
        PageTool pageTool = new PageTool(totalRecordCount, users.getCurrPageNo(), 3);
//        得到起始行数
        int startNum = pageTool.getStartRow();
//        得到每一页的显示数
        int pageSize = pageTool.getPageSize();

        users.setStarNum(startNum);
        users.setPageSize(pageSize);

        List<Users> list = usersService.getUsersByPage(users);
        List<UserType> plist = userTypeService.getAllusertypeList();

        model.addAttribute("list", list);
        model.addAttribute("plist", plist);
        model.addAttribute("users", users);
        model.addAttribute("pageTool", pageTool);

        model.addAttribute("prePages", pageTool.getPrePages());
        model.addAttribute("nextPages", pageTool.getNextPages());

        return "/users/list";


    }

}
